package com.housesales.web;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import module.Shop;
import module.Unit;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import bussinessService.basicbussinessService;
import bussinessService.impl.ShopBussinessServiceImpl;
import domain.vo.JSONObject;
import domain.vo.Message;
import domain.vo.ShopQueryObject;
import domain.vo.ShopVO;
import domain.vo.queryObject;

/**
 * 
* @ClassName: ShopController 
* @Description: 门店MVC
* @author chenchao
* @date 2014年1月16日 下午3:01:00 
*
 */
@Controller
@RequestMapping("/shop")
public class ShopController {
	private Logger logger = Logger.getLogger(ShopController.class.getName());
	/**
	 * 门店业务层实现类
	 */
	private basicbussinessService<Shop,ShopVO>  shopBIS = new ShopBussinessServiceImpl();
	
	
	@RequestMapping("/viewList")
	public ModelAndView viewList() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("admin/shop/shop");
		return mv;
	}
	
	@RequestMapping("/getAllShopJson")
	public @ResponseBody JSONObject<ShopVO> getAllShopJson() {
		JSONObject<ShopVO> jsonObject = new JSONObject<ShopVO>();
		
		try {
			List<ShopVO> tempVOS = new ArrayList<ShopVO>(); 
			tempVOS = shopBIS.getAll();
			jsonObject.setTotal(tempVOS.size());
			jsonObject.setRows(tempVOS);
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return jsonObject;
	}
	
	
	@RequestMapping("/getParentShopJson")
	public @ResponseBody JSONObject<ShopVO> getParentShopJson() {
		JSONObject<ShopVO> jsonObject = new JSONObject<ShopVO>();
		ShopQueryObject queryObject  = new ShopQueryObject();
		try {
			List<ShopVO> tempVOS = new ArrayList<ShopVO>(); 
			queryObject.setParentId("-1");
			tempVOS = shopBIS.getObjectByQueryObject(queryObject);
			jsonObject.setTotal(tempVOS.size());
			jsonObject.setRows(tempVOS);
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return jsonObject;
	}
	
	
	@RequestMapping("/getChildrenShopJson")
	public @ResponseBody JSONObject<ShopVO> getChildrenShopJson() {
		JSONObject<ShopVO> jsonObject = new JSONObject<ShopVO>();
		try {
			List<ShopVO> tempVOS = new ArrayList<ShopVO>(); 
			tempVOS = shopBIS.getAll();
			tempVOS.remove(0);
			tempVOS.remove(0);
			jsonObject.setTotal(tempVOS.size());
			jsonObject.setRows(tempVOS);
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return jsonObject;
	}
	
	
	@RequestMapping("/getShopTreeJson")
	public @ResponseBody JSONObject<ShopVO> getShopTreeJson() {
		JSONObject<ShopVO> jsonObject = new JSONObject<ShopVO>();
		queryObject queryObject  = new ShopQueryObject();
		try {
			List<ShopVO> tempVOS = new ArrayList<ShopVO>(); 
			tempVOS = shopBIS.getObjectByQueryObject(queryObject);
			jsonObject.setTotal(tempVOS.size());
			jsonObject.setRows(tempVOS);
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return jsonObject;
	}
	
	
	@RequestMapping("/addShop")
	public @ResponseBody  Message saveShop(@RequestParam("parentShopId1") int  parentShopId, Shop shop ) {
		Message ms = new Message();
		try {
			shop.setParentShopId(parentShopId);
			shopBIS.merageObject(shop);
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			ms.setErrorMsg("数据保存失败");
		}
		return ms;
	}
}
